﻿using Net_SCADA.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA
{
    public partial class Form_AlarmLevelSet : fdBaseForm
    {
        string[][] alarmAct = new string[3][] { new string[2] { "无", "None" }, new string[2] { "变色", "Change Color" }, new string[2] { "隔离", "Isolation" } };
        Font[] equipButtonFont= new Font[] { new Font("Microsoft YaHei", 12f), new Font("Arial", 12f) };


        Dictionary<string,Button> SelectButton=new Dictionary<string, Button>();
        DataTable AlarmData=new DataTable();
        DataRow[] rows;
        bool isLoading=false;
        public Form_AlarmLevelSet()
        {
            InitializeComponent();
        }

        private void Form_AlarmLevelSet_Load(object sender , EventArgs e)
        {
            AlarmData = DBOperate.GetAlarmSet();
            LoadInterface();
            LoadEquipButtons();
        }

        private void LoadInterface()
        {
            grd_Alarm.Columns[0].Width = 100;
            grd_Alarm.Columns[1].Width = grd_Alarm.Width -400;
            grd_Alarm.Columns[2].Width = 100;
            grd_Alarm.Columns[3].Width = 150;
        }

        private void LoadEquipButtons()
        {
            string[] alarmequip=DBOperate.GetAlarmEquipUsed();
            int _w=(pnl_Select.Width -(alarmequip.Length +1)*2)/alarmequip.Length;
            if (alarmequip == null || alarmequip.Length == 0) return;
            for (int i = 0 ; i < alarmequip.Length ; i++)
            {
                Button btn=new Button()
                {
                    Name= $"equip{i}",
                    Size=new Size(_w,pnl_Select.Height -4),
                    Text=NetLine.Equip[alarmequip[i]].EquipName[MultipleLanguage.systemLanguageIndex],
                    Font= equipButtonFont[MultipleLanguage.systemLanguageIndex],
                    FlatStyle=FlatStyle.Flat,
                    Location=new Point(2+i*(_w+2),2),
                    Tag= alarmequip[i],
                };
                SelectButton.Add(alarmequip[i] , btn);
                btn.Click += BtnAlarmEquip_Click;
                pnl_Select.Controls.Add(btn);
                MultipleLanguage.formLanguage[this.Name].Add(MultipleLanguage.getLanguageCtrl(
                    btn.Name, "Button", "Text", NetLine.Equip[alarmequip[i]].EquipName, "", equipButtonFont
                ));
            }
            lbl_Title.Text = NetLine.Equip[alarmequip[0]].EquipName[MultipleLanguage.systemLanguageIndex];
            ShowAlarmDetails(alarmequip[0]);
        }
        private void BtnAlarmEquip_Click(object sender , EventArgs e)
        {
            Button btn= sender as Button;
            lbl_Title.Text = btn.Text ;
            lbl_Title.Font = lblSubTitle.Font;
            lblSubTitle.Left= (int)(fdPublicFunction.GetTextSize(lbl_Title.Font, lbl_Title.Text).Width * 1.1f);
            ShowAlarmDetails(btn.Tag.ToString());
        }

        private void ShowAlarmDetails(string Equipid)
        {
            isLoading = true;
            if (AlarmData == null || AlarmData.Rows.Count == 0) return;
            rows= AlarmData.Select("equipid = " + Equipid);
            grd_Alarm.Rows.Clear();
            grd_Alarm.Rows.Add(rows.Length);
            for (int i = 0 ; i < rows.Length ; i++)
            {
                try
                {
                    DataGridViewComboBoxCell cmbCell= grd_Alarm.Rows[i].Cells[3] as DataGridViewComboBoxCell;
                    cmbCell.Items.Clear();
                    for (int k = 0; k < alarmAct.Length; k++)
                        cmbCell.Items.Add(alarmAct[k][MultipleLanguage.systemLanguageIndex]);
                    int lvl=Convert.ToInt32(rows[i]["allevel"]);
                    grd_Alarm.Rows[i].Cells[0].Value = rows[i]["alid"];
                    grd_Alarm.Rows[i].Cells[1].Value = rows[i]["alname"].ToString().Split(';')[MultipleLanguage.systemLanguageIndex];
                    grd_Alarm.Rows[i].Cells[2].Value = lvl.ToString();
                    grd_Alarm.Rows[i].Cells[3].Value = alarmAct[Convert.ToInt32(rows[i]["alAction"])][MultipleLanguage.systemLanguageIndex];
                    grd_Alarm.Rows[i].Cells[1].Style.BackColor = NetLine.AlarmLevelColor[lvl][1];
                    grd_Alarm.Rows[i].Cells[1].Style.ForeColor = NetLine.AlarmLevelColor[lvl][0];
                    if (lvl == 3)
                    {
                        grd_Alarm.Rows[i].Cells[3].ReadOnly = true;
                        grd_Alarm.Rows[i].Cells[3].Value = alarmAct[0][MultipleLanguage.systemLanguageIndex];
                    }
                }
                catch (Exception)
                {
                    continue;
                }
                
            }
            isLoading = false;
        }
        private void SaveAlarmDetails()
        {
            Dictionary<int,int[]> dict=new Dictionary<int, int[]>();
            DataTable dt=DBOperate.GetAlarmSet();
            if (dt == null || dt.Rows.Count == 0 || dt.Rows.Count!=AlarmData.Rows.Count) return;
            for (int i = 0 ; i < AlarmData.Rows.Count ; i++)
            {
                try
                {
                    if (dt.Rows[i]["allevel"].ToString() != AlarmData.Rows[i]["allevel"].ToString() || dt.Rows[i]["alaction"].ToString() != AlarmData.Rows[i]["alaction"].ToString())
                    {
                        dict.Add(Convert.ToInt32(AlarmData.Rows[i]["alid"]) , new int[2] { Convert.ToInt32(AlarmData.Rows[i]["allevel"]) , Convert.ToInt32(AlarmData.Rows[i]["alAction"]) });
                        NetLine.alarm[i].AlarmLevel = Convert.ToInt32(AlarmData.Rows[i]["allevel"]);
                        NetLine.alarm[i].AlarmAction = Convert.ToInt32(AlarmData.Rows[i]["alaction"]);
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }
            if (DBOperate.SaveAlarmLevelSet(dict)) {
                NetLine.isNeedFlashAlarm = true;
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[2], "", formLanguageIndex);
                //fdMessage.ShowMsg("警报等级设置成功！" , "警报设置" , 1 , 0);
            }
            else
                fdMessage.ShowMsg(MultipleLanguage.messageInfos[3], "", formLanguageIndex);
                //fdMessage.ShowMsg("警报等级设置失败，请检查数据库！" , "警报设置" , 1 , 0);

        }

        private void btn_Close_Click(object sender , EventArgs e)
        {
            this.Dispose();
        }

        private void btn_Save_Click(object sender , EventArgs e)
        {
             SaveAlarmDetails();
        }
        private void grd_Alarm_CellValueChanged(object sender , DataGridViewCellEventArgs e)
        {
            if (isLoading || grd_Alarm.CurrentCell==null) return;
            if (grd_Alarm.CurrentCell.ColumnIndex == 2)
            {
                rows[grd_Alarm.CurrentCell.RowIndex]["allevel"] = grd_Alarm.CurrentCell.Value;
                if (grd_Alarm.CurrentCell.Value.ToString() == "3")
                    grd_Alarm.Rows[grd_Alarm.CurrentCell.RowIndex].Cells[3].ReadOnly = true;
                else
                    grd_Alarm.Rows[grd_Alarm.CurrentCell.RowIndex].Cells[3].ReadOnly = false;

                grd_Alarm.Rows[grd_Alarm.CurrentCell.RowIndex].Cells[1].Style.BackColor = NetLine.AlarmLevelColor[Convert.ToInt32(grd_Alarm.CurrentCell.Value)][1];
                grd_Alarm.Rows[grd_Alarm.CurrentCell.RowIndex].Cells[1].Style.ForeColor = NetLine.AlarmLevelColor[Convert.ToInt32(grd_Alarm.CurrentCell.Value)][0];
            }
            if (grd_Alarm.CurrentCell.ColumnIndex == 3)
            {
                DataGridViewComboBoxCell cmbCell= grd_Alarm.CurrentCell as DataGridViewComboBoxCell;
                for (int i = 0; i < alarmAct.Length; i++)
                {
                    if (alarmAct[i][MultipleLanguage.systemLanguageIndex] == grd_Alarm.CurrentCell.Value.ToString())
                    {
                        rows[grd_Alarm.CurrentCell.RowIndex]["alAction"] = i;
                        break;
                    }
                }
                

                //rows[grd_Alarm.CurrentCell.RowIndex]["alAction"] =[grd_Alarm.CurrentCell.Value.ToString()];
            }
        }

        private void grd_Alarm_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}//update `set_alarm_siemens` set `alaction`=0;

